package com.boe.csb.web.controller.base;

import com.boe.csb.core.common.exception.CSBException;
import com.boe.csb.core.common.status.AdminUserRole;
import com.boe.csb.core.dao.UserOperateLogMapper;
import com.boe.csb.core.entity.dto.ApiCategoryDTO;
import com.boe.csb.core.entity.dto.QueryApiCategory;
import com.boe.csb.core.entity.po.ApiCategory;
import com.boe.csb.core.entity.po.UserOperateLog;
import com.boe.csb.core.service.api.ApiCategoryService;
import com.boe.csb.core.service.user.BoeUserService;
import com.boe.csb.web.controller.BaseController;
import com.boe.csb.web.model.ResultModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author tianxiang.luo
 */
@RestController
@RequestMapping(value = "api/category/")
public class ApiCategoryController extends BaseController {


    private Logger logger = LoggerFactory.getLogger(ApiCategoryController.class);
    @Autowired
    private ApiCategoryService apiCategoryService;
    @Autowired
    private BoeUserService boeUserService;
    @Autowired
    private UserOperateLogMapper userOperateLogMapper;
    /**
     * list/first  查看所有的一级目录
     */
    @RequestMapping(value = "list/first", method = RequestMethod.GET)
    public ResultModel<List<ApiCategory>> listFirst() {
        return ResultModel.success(apiCategoryService.queryAllFirstApiCategory());
    }


    /**
     * 新增服务类型
     *
     * @param apiCategory
     * @return
     */
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public ResultModel<Void> insertApiCategory(HttpSession session,@ModelAttribute ApiCategory apiCategory) {

        apiCategoryService.insertApiCategory(apiCategory);

        //纪录日志
        String userId = fetchUserId(session);
        String log = String.valueOf(apiCategory.getName());
        String userName = boeUserService.findBoeUser(userId).getEmail();
        String remark = String.format("管理员%s新增了类目:%s",userName,log);
        userOperateLogMapper.insert(new UserOperateLog(Long.valueOf(userId),userName,log, AdminUserRole.CATEGORY_MANAGER.getRole(),remark,new Date()));

        return ResultModel.success(null);
    }

    /**
     * 删除服务类型
     */
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public ResultModel<Boolean> deleteApiCategory(HttpSession session,@RequestParam Long id, @RequestParam Long parentId) {
            //纪录日志
            String userId = fetchUserId(session);
            String log = apiCategoryService.queryCategoryById(id).getName();
            String userName = boeUserService.findBoeUser(userId).getEmail();
            String remark = String.format("管理员%s删除了类目:%s",userName,log);
            userOperateLogMapper.insert(new UserOperateLog(Long.valueOf(userId),userName,log, AdminUserRole.CATEGORY_MANAGER.getRole(),remark,new Date()));

            return ResultModel.success(apiCategoryService.deleteApiCategory(id,parentId));
    }

    /**
     * 平行查询所有的api
     *
     * @param query
     * @return
     */
    @RequestMapping(value = "query",method = RequestMethod.GET)
    public ResultModel<List<ApiCategory>> queryApiCategoryList(QueryApiCategory query) {
        return ResultModel.success(apiCategoryService.queryApiCategoryList(query));
    }

    /**
     * 更新服务类型
     *
     * @param apiCategory
     * @return
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public ResultModel<Void> updateApiCategory(@ModelAttribute ApiCategory apiCategory) {
        apiCategoryService.updateApiCategory(apiCategory);
        return ResultModel.success(null);
    }

    /**
     * 请求一二级目录
     * @return
     */
    @GetMapping(value = "query/first/second")
    public ResultModel<List<ApiCategoryDTO>> queryFirstSecond(){
        return ResultModel.success(apiCategoryService.queryAllApiCategoryList());
    }


}
